package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.*;
import cn.itsource.pet.domain.PetType;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMessageDto;
import cn.itsource.pet.mapper.PetTypeMapper;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.PetTypeQuery;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.IPetTypeService;
import cn.itsource.pet.service.ISearchMasterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage, SearchMasterMessageQuery> implements ISearchMasterMessageService {

    @Autowired
    private SearchMasterMessageMapper messageMapper;

    @Autowired
    private SearchMasterMsgMapper msgMapper;

    /**
     * 审核操作
     * @param messageDto
     * @return
     */
    @Override
    public AjaxResult shenghe(SearchMessageDto messageDto) {
        //1.通过dto有审核表的id，通过id查询到审核表的数据对象
        Long id = messageDto.getId();
        SearchMasterMessage dbMessage = messageMapper.selectByPrimaryKey(id);

        //2.如果messageDto里面是驳回，只需要修改一下审核表的state = 2 就行了
        Integer state = messageDto.getState();
        if (state == 2){
            //设置为驳回的状态
            dbMessage.setState(2);
            //修改对象
            messageMapper.updateByPrimaryKeySelective(dbMessage);
            return AjaxResult.createSuccess();
        }

        //3.如果是审核通过
        //3.1需要修改一下审核表的state = 1 已审核
        dbMessage.setState(1);
        //修改对象
        messageMapper.updateByPrimaryKeySelective(dbMessage);

        //3.2把数据拷贝到接单对象，入库接单表
        //                                                          审核表       接单表
        SearchMasterMsg searchMasterMsg = BeanUtil.copyProperties(dbMessage, SearchMasterMsg.class);
        searchMasterMsg.setNote(messageDto.getNote());
        searchMasterMsg.setState(0);//待接单
        searchMasterMsg.setId(null);
        msgMapper.insertSelective(searchMasterMsg);

        return AjaxResult.createSuccess();
    }

    @Override
    public AjaxResult publish(SearchMasterMessage message) {

        //1.通过工具类获取userId
        Long userId = GetUserInfoUtil.getUserId();
        message.setUserId(userId);

        //2.入库
        messageMapper.insertSelective(message);


        return AjaxResult.createSuccess();
    }

    @Override
    public PageList<SearchMasterMessage> findMyPage(SearchMasterMessageQuery query) {
        //1.使用分页插件
        PageHelper.startPage(query.getPage(),query.getSize());

        //2.调用分页方法查询
        List<SearchMasterMessage> messageList = messageMapper.findMyPage(query);

        //3.封装成pageInfo对象
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(messageList);

        //4.返回自定义的PageList对象
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }


}
